<template>
  <el-dialog title="提示" :visible.sync="visible" width="30%">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px">
      <el-form-item label="给六鸽服务打分" prop="rate">
        <el-rate v-model="ruleForm.rate" show-text @change="(e) => (ruleForm.rate = e)"></el-rate>
      </el-form-item>
      <!-- <el-form-item label="标签" prop="tags">
        <el-tag
          :key="tag"
          v-for="tag in ruleForm.tags"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
          style="margin: 0 5px 5px 0"
        >{{ tag }}</el-tag>
        <span v-if="ruleForm.tags.length<10">
          <el-input
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
            :maxlength="20"
          ></el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
        </span>
      </el-form-item>-->
      <el-form-item prop="tags" label="标签">
        <el-checkbox-group v-model="ruleForm.tags">
          <el-checkbox :label="item.dictLabel" v-for="item in tags" :key="item.dictCode"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input type="textarea" v-model="ruleForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onCancel">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { apiDictOrderEvaluate } from "@/api/order";
export default {
  components: {},
  data() {
    return {
      tags: [],
      ruleForm: {
        tags: [],
        remark: "",
        rate: null
      },
      rules: {
        rate: [
          { required: true, message: "请评分", trigger: "blur" },
          { type: "number", min: 1, message: "请评分", trigger: "blur" }
        ],
        tags: [
          {
            type: "array",
            required: true,
            message: "请添加标签",
            trigger: "change"
          }
        ],
        remark: [{ required: true, message: "请填写描述", trigger: "blur" }]
      },
      inputVisible: false,
      inputValue: "",
      visible: false
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      apiDictOrderEvaluate().then(res => {
        this.tags = res.data;
      });
    },
    onShow() {
      this.visible = true;
    },
    onSubmit() {
      console.log(this.ruleForm, "aaa");
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          console.log(this.ruleForm, "售后");
          this.$emit("onRate", this.ruleForm);
          this.$nextTick(() => {
            this.onCancel();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    onCancel() {
      this.$refs.ruleForm.resetFields();
      this.visible = false;
    },
    handleClose(tag) {
      this.ruleForm.tags.splice(this.ruleForm.tags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.ruleForm.tags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    }
  }
};
</script>
